package com.chima.action;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Resource;
import com.chima.domain.Rose;
import com.chima.service.ResourceService;
import com.chima.service.RoseService;
import com.opensymphony.xwork2.ActionSupport;
import flexjson.JSONSerializer;

public class PowerAndRoseAction  extends ActionSupport{
	private ResourceService resourceService;
	
	
	private int id;
	private String rosename;
	private String name;
	private Integer rid;
	private String[] names;
	private String[] ids;
	private int roseid;
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
    private boolean success=false ;
	
	private List<Rose> roses;
	private List<Resource> resource;
	
	private RoseService roseService;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ResourceService getResourceService() {
		return resourceService;
	}
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public List<Rose> getRoses() {
		return roses;
	}
	public void setRoses(List<Rose> roses) {
		this.roses = roses;
	}
	
	public String[] getIds() {
		return ids;
	}
	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public int getRoseid() {
		return roseid;
	}
	public void setRoseid(int roseid) {
		this.roseid = roseid;
	}
	public List<Resource> getResource() {
		return resource;
	}
	public void setResource(List<Resource> resource) {
		this.resource = resource;
	}
	public RoseService getRoseService() {
		return roseService;
	}
	public void setRoseService(RoseService roseService) {
		this.roseService = roseService;
	}
	public String getRosename() {
		return rosename;
	}
	public void setRosename(String rosename) {
		this.rosename = rosename;
	}
	public Integer getRid() {
		return rid;
	}
	public void setRid(Integer rid) {
		this.rid = rid;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String[] getNames() {
		return names;
	}
	public void setNames(String[] names) {
		this.names = names;
	}
//	private static final long serialVersionUID = 1L;
	public String ListRose() throws Exception{
		PageRequestBean pageRequestBean = new PageRequestBean();
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = roseService.findrose(pageRequestBean);
		roses=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.serialize(roses);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",roses:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
		
		
	}
	public String  saveorupdate() throws Exception{
		roseService.delrose_resource(id);
		for(int i=0;i<names.length;i++){
//			Resource iResource=resourceService.findidbyname(names[i]);
			Resource iResource=resourceService.resourcesfindbyId(Integer.parseInt(ids[i]));
			if(roseService.saveorupdate(id,iResource.getId())){
				this.setSuccess(true);
				JsonConfig jsonConfig = new JsonConfig();
				JSONArray jsonArray = JSONArray.fromObject(success,jsonConfig);
				String jsonString = "{success:"+jsonArray.toString()+"}";
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
			}
		}
		
		return NONE;
	}
	public String addRole() throws Exception{
		Rose model=new Rose();
		model.setName(rosename);
		if( roseService.saveRose(model)){
			this.setSuccess(true);
			JsonConfig jsonConfig = new JsonConfig();
			JSONArray jsonArray = JSONArray.fromObject(success,jsonConfig);
			String jsonString = "{success:"+jsonArray.toString()+"}";
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
		 
		 return NONE; 
	 }
	public String editRole() throws Exception{
			  Rose model=new Rose();
				model.setName(name);
				model.setId(id);
				roseService.edit(model);
			this.setSuccess(true);
			JsonConfig jsonConfig = new JsonConfig();
			JSONArray jsonArray = JSONArray.fromObject(success,jsonConfig);
			String jsonString = "{success:"+jsonArray.toString()+"}";
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		
		 
		 return NONE; 
	 }

	public String deleteRose() throws Exception{
		roseService.deleteRose(rid);
		this.setSuccess(true);
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(success,jsonConfig);
		String jsonString = "{success:"+jsonArray.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String Treelist() throws Exception{
		resource=resourceService.ListAllResource();
				if(resource.size()>0){
					String json="[";
					for(int i=0;i<resource.size();i++){
							if(resource.get(i).getPid()==0){
									Boolean check1=roseService.check(resource.get(i).getId(), roseid);
									json+="{'id':'"+resource.get(i).getId()+"','text':'"+resource.get(i).getName()+"','expanded':true,'checked':"+check1+"";
									 List<Resource> SonResource=resourceService.findreResourcesByPid(resource.get(i).getId());
										if(SonResource.size()>0){
											json+=",children:[";
											  for(int j=0;j<SonResource.size();j++){
													  Boolean leaf=false;
													  Boolean check2=roseService.check(SonResource.get(j).getId(), roseid);
													  List<Resource> datesource=resourceService.findreResourcesByPid(SonResource.get(j).getId());
													  if(datesource.size()==0){
														  leaf=true;
														  json+="{'id':'"+SonResource.get(j).getId()+"','text':'"+SonResource.get(j).getName()+"','leaf':'"+leaf+"','checked':"+check2+"},"; 		
													  }else{
														  json+="{'id':'"+SonResource.get(j).getId()+"','text':'"+SonResource.get(j).getName()+"','leaf':'"+leaf+"','expanded':true,'checked':"+check2+""; 
														  json+=",children:[";
																 for(int k=0;k<datesource.size();k++){
																		  Boolean check3=roseService.check(datesource.get(k).getId(), roseid);
																		  List<Resource> sondatesource=resourceService.findreResourcesByPid(datesource.get(k).getId());
																		  if(sondatesource.size()==0){
																			  leaf=true;
																			  json+="{'id':'"+datesource.get(k).getId()+"','text':'"+datesource.get(k).getName()+"','leaf':'"+true+"','checked':"+check3+"},"; 
																		  }else{
																			  json+="{'id':'"+datesource.get(k).getId()+"','text':'"+datesource.get(k).getName()+"','leaf':'"+leaf+"','expanded':true,'checked':"+check3+"";
																			  json+=",children:[";
																			  for(int w=0;w<sondatesource.size();w++){
																				  Boolean check4=roseService.check(sondatesource.get(w).getId(), roseid);
																				  json+="{'id':'"+sondatesource.get(w).getId()+"','text':'"+sondatesource.get(w).getName()+"','leaf':'"+true+"','checked':"+check4+"},"; 
																			  }
																			  json=json.substring(0,json.length()-1);
																			  json+="]},";
																		  }    
																  }
													    json=json.substring(0,json.length()-1);
													    json+="]},";
												  }
											  }
											  json=json.substring(0,json.length()-1);
											  json+="]},";
										}
								} 
					
				}
					json=json.substring(0,json.length()-1);
					json+="]";
				 if(roseid!=0){
					 ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
					 ServletActionContext.getResponse().getWriter().print(json);
				 }
					
			}
			return NONE;
    }
}
